<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/14 0014
 * Time: 9:34
 */

//添加接口请求日志
function add_interface_request_log($params, $action, $member_ip = '') {
    if(!$member_ip) return ['data'=>'add_interface_request_log','code'=>400001,'msg'=> 'FAULT'];
    //$log['app_id'] = $param['app_id'];
    $log['interface'] = $action;
    $log['request_time'] = time();
    $log['member_ip'] = $member_ip;
    if(isset($params['member_id'])) $log['member_id'] = $params['member_id'];
    $rs = db('interface_request_log')->insert($log);
    if(!$rs)
        return ['data'=>'','code'=>400067,'msg'=> 'FAULT'];
    else
        return ['data'=>'','code'=>200,'msg'=> 'SUCCESS'];
}


// 接口身份验证
function check_merchant($params) {
    $sign = $params['sign'];
    $timestamp = $params['timestamp'];
    $key = 'jgsdfjkfgjs#$%&khs2423hkfh';

    if((time() - $timestamp) > 600) {
        return ['data'=>'','code'=>400004,'msg'=>'FAULT'];
    }

    $check_sign = md5('timestamp=' . $timestamp . '&key=' . $key);

    if($check_sign != $sign) {
        return ['data'=>'','code'=>400005,'msg'=>'FAULT'];
    }
    return ['data'=>'','code'=>200,'msg'=>'SUCCESS'];
}

// 上传身份证
// $data  原数据 身份证上传完成之后需要合并到原数据里面
// $action_dir 图片保存的目录
function upload_card($data, $action_dir) {
    $files = request()->file('card');
    $id_card_in_hand = $id_card_front = $id_card_verso = '';
    if($files) {
        $i = 1;
        foreach ($files as $k => $file) {
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . $action_dir);
            if ($info) {
                // 输出 图片地址
                switch ($i) {
                    case 1:
                        $id_card_in_hand = $action_dir . DS . $info->getSaveName();
                        break;
                    case 2:
                        $id_card_front = $action_dir . DS . $info->getSaveName();
                        break;
                    case 3:
                        $id_card_verso = $action_dir . DS . $info->getSaveName();
                        break;
                    default:
                        break;
                }
            } else {
                // 上传失败, 返回错误信息
                //return json(['data' => '', 'code' => 400010, 'msg' => ]);
                return json(['data' => '', 'code' => 400010, 'msg' => $file->getError()]);
            }
            $i++;
        }
    }
    if(!$id_card_in_hand || !$id_card_front || !$id_card_verso) {
        return json(['data' => '', 'code' => 400070, 'msg' => '']);
    }
    $data = array_merge_recursive($data, ['id_card_in_hand' => $id_card_in_hand, 'id_card_front' => $id_card_front, 'id_card_verso' => $id_card_verso]);
    return json(['data' => $data, 'code' => 200, 'msg' => '']);;
}


/**
 * 给二维码添加logo
 * @param $qr_code_url  (二维码图片地址
 * @param string $logo  (默认值是一张图片
 */
function erweima_add_log_($qr_code_url, $logo = '') {
    $logo = $logo ? $logo : ROOT_PATH . 'public' . DS . 'static' . DS . 'images' . DS . 'logo.png';
    $QR = $qr_code_url;//已经生成的原始二维码图

    if ($logo !== FALSE) {
        $QR = imagecreatefromstring(file_get_contents($QR));
        $logo = imagecreatefromstring(file_get_contents($logo));
        $QR_width = imagesx($QR);//二维码图片宽度
        $QR_height = imagesy($QR);//二维码图片高度
        $logo_width = imagesx($logo);//logo图片宽度
        $logo_height = imagesy($logo);//logo图片高度
        $logo_qr_width = $QR_width / 5;
        $scale = $logo_width/$logo_qr_width;
        $logo_qr_height = $logo_height/$scale;
        $from_width = ($QR_width - $logo_qr_width) / 2;
        //重新组合图片并调整大小
        imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
            $logo_qr_height, $logo_width, $logo_height);
    }
//输出图片
    imagepng($QR, $qr_code_url);
}


// 添加站内信
// $members 用户id数组，全部用户传入 all
// $type 站内信类型 1：系统； 2：订单； 3：活动；
// $msg 站内信内容
// $title 站内信标题；
function create_message($members, $type, $msg, $title) {
    $data = [
        'type' => $type,
        'msg' => $msg,
        'title' => $title,
        'status' => 1,
        'created_at' => time(),
    ];

    if ($members == 'all'){
        $members = db('member')->column('id');
    } else {
        if (!is_array($members))
            return false;
    }
    $messages = [];
    foreach ($members as $id) {
        $data['member_id'] = $id;
        $messages[] = $data;
    }
    $rs = db('member_message')->insertAll($messages);
    return $rs;
}

// 获取个人信息字段对应的中文
function get_member_role_info_title($field) {
    $data = [
        'nickname' => '昵称',
        'sex' => '性别',
        'birthday' => '生日',
        'phone' => '手机',
        'signature' => '个性签名',
        'headimgurl' => '头像'
    ];
    return $data[$field];
}

